#include "sm_queue.h"

static queue<int> my_sm_queue;
const int my_sm_queue_max_size = 5;

int sm_queue(IN int action_id, OUT SM_QUEUE_STATE_ENUM *queue_state)
{
    int iResult = OK;
    if(action_id <= 0)
    {
        iResult = SM4A_QU_PARAMETERS_ERROR;
    }
    if(iResult == OK)
    {
        if(queue_state == nullptr)
        {
            iResult = SM4A_NULL_POINTER_ERROR;
        }
    }

    int current_size = my_sm_queue.size();
    if(iResult == OK)
    {
        if(current_size >= my_sm_queue_max_size)
        {
            iResult = SM4A_PRECONDITION_ERROR;
        }else if( (current_size+1) == my_sm_queue_max_size)
        {
            *queue_state = QUEUE_STATE_FULL;
        }else
        {
            *queue_state = QUEUE_STATE_PARTY_FULL;
        }
    }
    if(iResult == OK)
    {
        my_sm_queue.push(action_id);
    }

    return iResult;
}

int sm_get_result(OUT int *action_id, OUT SM_QUEUE_STATE_ENUM *queue_state)
{
    int iResult = OK;

    if(iResult == OK)
    {
        if(queue_state == NULL || action_id == NULL)
        {
            iResult = SM4A_NULL_POINTER_ERROR;
        }
    }

    int current_size = my_sm_queue.size();
    if(iResult == OK)
    {
        if(current_size <= 0)
        {
            iResult = SM4A_PRECONDITION_ERROR;
        }else if( (current_size-1) == 0)
        {
            *queue_state = QUEUE_STATE_EMPTY;
        }else
        {
            *queue_state = QUEUE_STATE_PARTY_FULL;
        }
    }
    if(iResult == OK)
    {
        *action_id = my_sm_queue.front();
        my_sm_queue.pop();
    }

    return iResult;
}

int sm_queue_create()
{
    int iResult = OK;

    while(! my_sm_queue.empty())
    {
        my_sm_queue.pop();
    }

    return iResult;
}

int sm_queue_destory()
{
    int iResult = OK;
    while(! my_sm_queue.empty())
    {
        my_sm_queue.pop();
    }
    return iResult;
}

int sm_get_queue_size(int *size)
{
    int iResult = OK;

    if(iResult == OK)
    {
        if(size == nullptr)
        {
            iResult = SM4A_NULL_POINTER_ERROR;
        }
    }

    if(iResult == OK)
    {
        *size = my_sm_queue.size();
    }

    return iResult;
}

int sm_get_queue_max_size(int *size)
{
    int iResult = OK;

    if(iResult == OK)
    {
        if(size == nullptr)
        {
            iResult = SM4A_NULL_POINTER_ERROR;
        }
    }

    if(iResult == OK)
    {
        *size = my_sm_queue_max_size;
    }

    return iResult;
}
